package com.niubility.controller;


import com.niubility.entity.Message;
import com.niubility.exception.CustomException;
import com.niubility.server.Impl.MessageServer;
import com.niubility.vo.AssSendToUsers;
import com.niubility.vo.MessageShow;
import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@Transactional
public class MessageController {
    @Resource
    public MessageServer messageServer;

    Logger logger = Logger.getLogger(MessageController.class);

    //测试通过
//    @RequestMapping("/MessageByldAll")
//    //显示用户所有消息
//    public void ByldAll(HttpServletRequest request, HttpServletResponse response) {
//        response.setCharacterEncoding("UTF-8");
//        List<Message> list = null;
//        String callback = request.getParameter("callback");
//        int mesMainId = Integer.parseInt(request.getParameter("mesMainid"));
//        DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh-mm");
//        String date = format.format(new Date());
//        String result = "";
//        List<MessageArt> list1 = new ArrayList<MessageArt>();
//        try{
//            list = messageServer.ByldAll(mesMainId);
//            for(Message m:list){
//                MessageArt messageArt = new MessageArt();
//                messageArt.setArtId(Integer.parseInt(m.getMesText().substring(1,10)));
//                messageArt.setMesText(m.getMesText().substring(11,m.getMesText().length()-1));
//                messageArt.setMesDate(m.getMesDate());
//                messageArt.setMesSendid(m.getMesSendid());
//                messageArt.setMesMainid(m.getMesMainid());
//                list1.add(messageArt);
//            }
//        }catch (Exception e ){
//            result = callback+"({" +"\"status:\"" +108+ "})";
//        }
//        result = callback+"(" + JSONArray.fromObject(list1).toString() + ")";
//        try {
//            response.getWriter().write(result);
//        } catch (IOException e) {
//            logger.debug("Error."+e);
//        }
//
//    }

    /*
    * @Author:jx
    * @Description: select by resive ID
    * @Date: John 2018/2/3
    * @Params:
    * @Return:
    * test pass
    */
    @RequestMapping("/MessByAss")
    public void SelectAss(HttpServletRequest request,HttpServletResponse response) throws CustomException {
        response.setCharacterEncoding("utf-8");
        int mainId = 0;
        String callback = request.getParameter("callback");
        String result=null;
        try {
            mainId = Integer.parseInt(request.getSession().getAttribute("userId").toString());
        }catch (Exception e){
            result = callback+"({"+"\"returncode\""+":"+103+"})";
            try {
                response.getWriter().write(result);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            throw new CustomException();
        }


        //System.out.println(mainId+" "+callback);
        try{
            List<MessageShow> messageList = messageServer.MessageSelect(mainId,2);
            List<AssSendToUsers> user = new ArrayList<AssSendToUsers>();
            for (MessageShow a:messageList) {
                String mess[] = a.getMesText().split("#");
                for (int i = 0;i<mess.length;i++){
                    //System.out.print(mess[i]+"-");
                }
                AssSendToUsers assSendToUsers = new AssSendToUsers();
                assSendToUsers.setUserId(Integer.parseInt(mess[1]));
                assSendToUsers.setUserName(mess[2]);
                assSendToUsers.setUserPic(mess[3]);
                assSendToUsers.setArtId(Integer.parseInt(mess[4]));
                assSendToUsers.setArtTitle(mess[5]);
                assSendToUsers.setAssText(mess[6]);
                assSendToUsers.setShowTop(mess[7]);
                assSendToUsers.setAssDate(a.getMesDate());
                user.add(assSendToUsers);
            }
            result = callback+"("+JSONArray.fromObject(user).toString()+")";
            //System.out.println(result);
        }catch (Exception e){
            logger.debug("数据异常."+e);
            throw new CustomException();
        }
        try {
            response.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /*
    * @Author:jx
    * @Description: save massage
    * @Date: John 2018/2/3
    * @Params:
    * @Return:
    * test pass
    */
    @RequestMapping("/MessageSave")
    public void Save(HttpServletRequest request, HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
        Message message = new Message();
        String callback = request.getParameter("callback");
        String result = null;
        int mainId =0;
        try {
            mainId = Integer.parseInt(request.getSession().getAttribute("userId").toString());
        }catch (Exception e){
            result = callback+"({"+"\"returncode\""+":"+103+"})";
            try {
                response.getWriter().write(result);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

        int sendId = Integer.parseInt(request.getParameter("sendId"));
        String text = request.getParameter("mesText");
        int mesassessmark = Integer.parseInt(request.getParameter("mesAssessMark"));
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        String date = format.format(new Date());

        message.setMesSendid(sendId);
        message.setMesMainid(mainId);
        message.setMesDate(date);
        message.setMesText(text);
        message.setMesAssessmark(mesassessmark);

        try{
            int i = messageServer.Save(message);
        }catch (Exception e){
            result=callback+"({"+"\"status\":"+108+"})";
        }
        result = callback + "({" + "\"state\":" +100+ "})";
        try {
            response.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//一键清除,yemianqingchu
//    @RequestMapping("/MessageDeleteAll")
//    public void DeleteAll(HttpServletRequest request,HttpServletResponse response){
//        response.setCharacterEncoding("utf-8");
//        Message message=new Message();
//         String callBack=request.getParameter("callback");
//         response.setCharacterEncoding("utf-8");
//         int mesMainid=Integer.parseInt(request.getParameter("mesMainid"));
//        try{
//            int i=messageServer.DeleteAll(mesMainid);
//        }catch (Exception e){
//            logger.debug("Error."+e);
//            String result="("+"{\"status\":"+108+"})";
//        }
//         String result="("+"{\"status\":"+100+"})";
//        try {
//            response.getWriter().write(callBack+result);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
}



